/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ftpcombiner.server.virtualfiles;

import ftpcombiner.filesystem.FileSystem;
import ftpcombiner.server.serverconnection.ServerItem;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.NoSuchElementException;
import javax.swing.tree.TreeNode;

/**
 *
 * @author Gary
 */
public abstract class VirtualItem<A extends ServerItem> implements TreeNode {

    protected A alias;
    protected VirtualFolder parent;
    protected int index = -1;

    public VirtualItem(A alias, VirtualFolder parent) {
        this.alias = alias;
        this.parent = parent;
    }

    public String getName() {
        return alias == null ? "/" : alias.getName();
    }

    /**
     * Gets the parent folder of this item. Returns
     * <code>null</code> if the item is in the root directory.
     */
    @Override
    public VirtualFolder getParent() {
        return parent;
    }

    public String getPath() {
        String path = ((parent == null ? true : parent.getAlias() == null && parent.getParent() == null) ? "" : parent.getPath()) + "/" + (alias == null ? "" : getName());
        return path;
    }

    public VirtualItem[] getPathAsArray() {
        VirtualItem dir = this;
        int i = 1;
        while (!dir.equals(FileSystem.getInstance().getRootDir())) {
            dir = dir.getParent();
            i++;
        }
        VirtualItem[] path = new VirtualItem[i];
        dir = this;
        for (int o = i; o > 0; o--) {
            path[o] = dir;
            dir = dir.getParent();
        }
        return path;
    }

    public A getAlias() {
        return alias;
    }

    @Override
    public String toString() {
        return getName();
    }

    /**
     * Gets the index of the item. The index is only ever reported correctly if
     * this item was just returned by
     * {@link VirtualFolder#getChild(java.lang.String)} or
     * {@link VirtualFolder#getChildAt(int)}. If the index is not defined this
     * method will return -1.
     *
     * @return The index of the item, or -1 if it has not been defined
     */
    public int getIndex() {
        return index;
    }

    protected class ArrayEnumeration implements Enumeration, Iterator {

        protected int _currentIndex_, _lastIndex_;
        protected Object[] _array_;

        public ArrayEnumeration(Object[] array, int startIndex, int length) {
            _currentIndex_ = startIndex;
            _lastIndex_ = startIndex + length - 1;
            _array_ = array;
        }

        public ArrayEnumeration(Object[] array) {
            this(array, 0, array.length);
        }

        @Override
        public boolean hasMoreElements() {
            return (_currentIndex_ <= _lastIndex_);
        }

        @Override
        public boolean hasNext() {
            return hasMoreElements();
        }

        @Override
        public Object nextElement() {
            if (!hasMoreElements() || _currentIndex_ < 0
                    || _currentIndex_ >= _array_.length) {
                throw new NoSuchElementException();
            }
            return _array_[_currentIndex_++];
        }

        @Override
        public Object next() {
            return nextElement();
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    }
}
